package com.zh.note.huawei.onehundred.勾股数元组;

import java.util.*;
import java.util.stream.Collectors;

/**
 * https://dream.blog.csdn.net/article/details/129191549
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // int value1 = sc.nextInt();
        // int value2 = sc.nextInt();
        int value1 = 1;
        int value2 = 20;
        //存储所有符合的勾股数
        List<List<Integer>> list = new ArrayList<>();

        for (int i = value1; i <= value2; i++) {
            for (int j = i + 1; j <= value2; j++) {
                if (check(i, j, value2)) { // 判断i,j是否符合勾股定理
                    List<Integer> tempList = new ArrayList<>();
                    tempList.add(i);
                    tempList.add(j);
                    tempList.add((int) Math.sqrt(i * i + j * j));
                    Collections.sort(tempList);
                    list.add(tempList);

                }
            }
        }
        //遍历list把不符合要求的删除掉
        // for (int i = 0; i < list.size(); i++) {
        //     List<Integer> temp = list.get(i);
        //     if (valid(temp.get(0), temp.get(1), temp.get(2))) {
        //         list.remove(i);
        //     }
        // }
        //去掉互为质数
        List<List<Integer>> collect = list.stream().filter(item -> {
            return !valid(item.get(0), item.get(1), item.get(2));
        }).collect(Collectors.toList());
        //输出最终结果
        for (int i = 0; i < collect.size(); i++) {
            List<Integer> temp = collect.get(i);
            System.out.println(temp.get(0) + " " + temp.get(1) + " " + temp.get(2));
        }
    }

    // 判断是否互为质数
    private static boolean valid(int m, int n, int k) {

        for (int i = 2; i < k; i++) {

            if (m % i == 0 && n % i == 0) { // m,n都可以被i整除
                return true;
            } else if (m % i == 0 && k % i == 0) {
                return true;
            } else if (n % i == 0 && k % i == 0) {
                return true;
            }
        }
        return false;
    }

    private static boolean check(int i, int j, int max) {
        double sqrt = Math.sqrt(i * i + j * j);
        int temp = (int) sqrt;
        return temp <= max && j < temp && sqrt * sqrt == temp * temp;
    }
}
